import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLStringParser {
    String xmlString =
        "<?xml version=\"1.0\" encoding=\"utf-8\"?> "
            + "<sentinel_ldk:license schema_version=\"1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:sentinel_ldk=\"http://www.safenet-inc.com/sentinelldk\">"
            + "   <acknowledgement_request>Yes</acknowledgement_request>"
            + "   <enforcement_type>HL</enforcement_type>"
            + "   <product>"
            + "      <id>0</id>"
            + "      <name>iHOS</name>"
            + "      <feature>"
            + "         <id>1</id>"
            + "         <name>iHOS</name>"
            + "         <license_properties>"
            + "            <perpetual/>"
            + "            <remote_desktop_access>No</remote_desktop_access>"
            + "         </license_properties>"
            + "      </feature>"
            + "   </product>"
            + "   <memory>"
            + "      <ro_memory_segment>"
            + "         <offset>0</offset>"
            + "         <content>MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjw1r7B+szaAACw5sioy/nT0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==</content>"
            + "      </ro_memory_segment>" + "      <rw_memory_segment>" + "         <offset>0</offset>"
            + "         <content>MDEAAAAAAAAtMQAAMDIAAAAAAAAtMQAAMDMAAAAAAAAtMQAA</content>" + "      </rw_memory_segment>" + "   </memory>"
            + " </sentinel_ldk:license>";

    @Test
    public void test() {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = null;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse( new ByteArrayInputStream( xmlString.getBytes() ) );
            // Document document = DocumentHelper.parseText(xmlString);
            doc.getDocumentElement().normalize();
            System.out.println( "Root element :" + doc.getDocumentElement().getNodeName() );
            NodeList nList = doc.getElementsByTagName( "ro_memory_segment" );
            for ( int temp = 0; temp < nList.getLength(); temp++ ) {
                Node nNode = nList.item( temp );
                if ( nNode.getNodeType() == Node.ELEMENT_NODE ) {
                    Element eElement = (Element) nNode;
                    System.out.println( "offset : " + eElement.getElementsByTagName( "offset" ).item( 0 ).getTextContent() );
                    System.out.println( "content : " + eElement.getElementsByTagName( "content" ).item( 0 ).getTextContent() );
                }

            }
             nList = doc.getElementsByTagName( "rw_memory_segment" );
            for ( int temp = 0; temp < nList.getLength(); temp++ ) {
                Node nNode = nList.item( temp );
                if ( nNode.getNodeType() == Node.ELEMENT_NODE ) {
                    Element eElement = (Element) nNode;
                    System.out.println( "offset : " + eElement.getElementsByTagName( "offset" ).item( 0 ).getTextContent() );
                    System.out.println( "content : " + eElement.getElementsByTagName( "content" ).item( 0 ).getTextContent() );
                }
                
            }

        }
        catch ( SAXException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch ( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch ( ParserConfigurationException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // fail( "Not yet implemented" );
    }

}
